package org.javaboy.redis.common.impl;

import org.javaboy.redis.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

//@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void set(Object key, Object value) {
        ValueOperations valueOperations=stringRedisTemplate.opsForValue();
        valueOperations.set(key,value);
    }

    @Override
    public void set(Object key, Object value, long timeout, TimeUnit unit) {
        ValueOperations valueOperations=stringRedisTemplate.opsForValue();
        valueOperations.set(key,value,timeout,unit);
    }

    @Override
    public Object get(Object key) {
        ValueOperations valueOperations=stringRedisTemplate.opsForValue();
        return valueOperations.get(key);
    }

    @Override
    public Boolean delete(Object key) {
        return stringRedisTemplate.delete(key.toString());
    }

    @Override
    public Long delete(Collection keys) {
        return stringRedisTemplate.delete(keys);
    }

    @Override
    public Boolean hasKey(Object key) {
        return stringRedisTemplate.hasKey(key.toString());
    }

    @Override
    public Boolean expire(Object key, long timeout, TimeUnit unit) {
        return stringRedisTemplate.expire(key.toString(),timeout,unit);
    }

    @Override
    public Long getExpire(Object key) {
        return stringRedisTemplate.getExpire(key.toString());
    }

    @Override
    public Boolean setIfAbsent(Object key, Object value, long timeout, TimeUnit unit) {
        ValueOperations valueOperations=stringRedisTemplate.opsForValue();
        return valueOperations.setIfAbsent(key,value,timeout,TimeUnit.MINUTES);
    }
}
